package li_cheng_can;

/**
 * @CLASSDESCRIPTION: 利用链表实现队列
 * @Author: LiChengCan
 * @Phone: 13237427206
 * @email: 929825769@qq.com
 * @Date: 2021/1/23 / 16:42
 **/
public class LI_CHENG_CAN_TH_Q1<T>{

    private Node<T> head;
    private int size;

    public LI_CHENG_CAN_TH_Q1(){
        //构造器初始化节点,size大小
        this.head = null;
        this.size = 0;
    }

    //判断链表是否为空
    public boolean isEmpty(){
        return this.size==0;
    }

    //返回链表长度
    public int size(){
        return this.size;
    }

    //添加方法（入栈）
    public void push(Object value){
        //创建一个新节点
        Node node = new Node(value);
        //如果栈为空,直接将node节点赋给 head头
        if (size == 0){
            head = node;
        }else {
            //不为空,则在node.next 后面
            node.next = head;// head =3 将head 付给 node.next  // head=node
            head = node;// 始终让新添加进来的数据处于head位置
        }
        size++;
    }

    //删除方法（出栈）
    public Object pop(){
        if (size == 0) return null;
        Object obj = head.value;
        head = head.next;
        size--;
        return obj;
    }
    //正向打印链表
    public void display(){
        if(size == 0) System.out.println("链表为空！");
        Node n = head;
        while(n != null){
            System.out.print(n.value+" __ ");
            n = n.next;
        }

    }
//将栈制空
    public boolean clear(){
        this.head = null;
        this.size = 0;
        return true;
    }

    class Node<T>{
        private T value;
        private Node next;

        public Node(T value){
            this.value = value;
        }
    }

    public static void main(String[] args) {
        LI_CHENG_CAN_TH_Q1<Integer> myStack = new LI_CHENG_CAN_TH_Q1<>();
        myStack.push(1);
        myStack.push(2);
        myStack.push(3);
        myStack.push(4);
        Object o = myStack.pop();
        System.out.println("已经出栈:"+o);
        myStack.display();
    }
}
